package com.example.forum.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.forum.entity.TDatadictionaries;
import com.example.forum.mapper.TDatadictionariesMapper;
import com.example.forum.service.ITDatadictionariesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author why
 * @since 2019-10-30
 */
@Service
public class TDatadictionariesServiceImpl extends ServiceImpl<TDatadictionariesMapper, TDatadictionaries> implements ITDatadictionariesService {
    @Resource
    private TDatadictionariesMapper datamapper;

    public List<TDatadictionaries> findByExpression(String expression) {
        if (expression.contains(">")) {
            var names = expression.split(">");
            switch (names.length) {
                case 1:
                    return findByParentName(names[0]);
                case 2:
                    try {
                        return findByName(names[0], names[1]);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                default:
                    return null;
            }
        } else {
            return findByParentName(expression);
        }

    }
    public  List<TDatadictionaries> findByName(String pid,String name)
    {


        var dataDictionary = super.list(new QueryWrapper<TDatadictionaries>().like("name", name).
                and(true,i->i.like("parentId", pid).
                        or().isNull("parentId")));

        return dataDictionary;

    }


    public List<TDatadictionaries> findByParentName(String name)
    {

        var dataDictionary = super.getOne(new QueryWrapper<TDatadictionaries>().like("name", name));
        if(dataDictionary!=null)
        {
            var childDataDictionary = super.list(new QueryWrapper<TDatadictionaries>().like("parentId", dataDictionary.getId()));

            return childDataDictionary;
        }
        return null;

    }

    public List<Object> GetAllDataDicForTree(){

        return datamapper.GetAllDataDicForTree();
    }
}
